package com.wn.date20190807FingerGuessingGame;

import java.util.Scanner;

/**
 * 游戏界面
 * 
 * @author song
 *
 */
public class Game
{
	Scanner scanner = new Scanner(System.in); // 系统输入
	Player pc = null; // 电脑
	Player user = null;// 用户
	int playTimes=0;

	/**
	 * 开场动画
	 */
	public void initial()
	{
		System.out.println("===========================欢迎进入猜拳==========================" + "=========\n\n\n\n");
		System.out.println("                             ***************                    ");
		System.out.println("                             **  猜拳，开始**                          ");
		System.out.println("                             ***************                    \n\n\n\n\n\n");
		gameRules();
	}

	/**
	 * 游戏规则
	 */
	public void gameRules()
	{
		while (true)
		{
			System.out.println("游戏规则：1.石头 2.剪刀 3.布");
			System.out.println("请选择对手：1.刘备 2.曹操 3.孙权：");
			int role = scanner.nextInt();
			switch (role)
			{
			case 1:
				pc = new Player("刘备");
				break;
			case 2:
				pc = new Player("曹操");
				break;
			case 3:
				pc = new Player("孙权");
				break;
			default:
				pc = new Player("xx");
				break;
			}
			System.out.println("您选择了" + pc.getName() + "进行对战");
			// 用户名
			System.out.println("请输入你的名字");
			user = new Player(scanner.next());
			System.out.println(user.getName() + "准备好猜拳了吗？（y/n）");
			if (scanner.next().toLowerCase().equals("y"))
			{
				break;
			}
		}
		startGame();
	}

	/**
	 * 进行游戏
	 */
	public void startGame()
	{
		while (true)
		{
			System.out.println("请出拳：1.石头 2.剪刀 3.布");
			String userOption = user.showFirst1(scanner.nextInt());
			System.out.println("你出：" + userOption);
			String pcOption = pc.showFirst1();
			System.out.println(pc.getName() + "出：" + pcOption);
			// 判断输赢
			int result=isWon(userOption, pcOption);
			if (result==0)
			{
				System.out.println("结果：平局");
			}else if(result>0){
				System.out.println("结果：你赢了");
				user.setPoint(user.getPoint()+1);
			}
			else {
				System.out.println("结果：输了");
				pc.setPoint(pc.getPoint()+1);
			}
			playTimes++;
			System.out.println("是否继续（y/n）:");
			// 不继续
			if(!scanner.next().toLowerCase().equals("y"))
			{
				break;
			}
		}
		showResult();
		
	}

	/**
	 * 输出结果
	 */
	public void showResult()
	{
		// TODO Auto-generated method stub
		System.out.println("---------------------------------------");
		System.out.println(user.getName()+"   VS   "+pc.getName());
		System.out.println("对战次数："+playTimes);
		System.out.println(user.getName()+"得分："+user.getPoint());
		System.out.println(pc.getName()+"得分："+pc.getPoint());
		if(user.getPoint()>pc.getPoint())
		{
			System.out.println("你太棒了！");
		}else {
			System.out.println("继续努力吧！");
		}
		
	}

	/**
	 * 比价输赢
	 * 
	 * @param userOption
	 * @param pcOption
	 * @return 0代表平局，-1输，1赢
	 */
	public int isWon(String userOption, String pcOption)
	{
		if (userOption.equals(pcOption))
		{
			return 0;
		} else if (userOption.equals("石头"))
		{
			if (pcOption.equals("布"))
			{
				return -1;
			} else
			{
				return 1;
			}
		} else if (userOption.equals("剪刀"))
		{
			if (pcOption.equals("布"))
			{
				return 1;
			} else
			{
				return -1;
			}
		} else// 布
		{
			if (pcOption.equals("石头"))
			{
				return 1;
			} else
			{
				return -1;
			}
		}

	}
}
